<template>
  <div>
    <!-- 搜索栏 -->
    <search-bar @onSearch="onSearch" />
    <el-button @click="showAddModal" type="primary" style="margin:15px 0">新增商品分类</el-button>
    <!-- /搜索栏 -->
    <el-table row-key="id" :data="cates" style="width: 100%">
      <el-table-column prop="cateName" label="分类名" width="180" />
      <el-table-column prop="cateIcon" label="分类图标">
        <template #default="{ row }">
          <div>
            <img :src="row.cateIcon" width="50" alt="">
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="enable" label="是否启用">
        <template #default="{ row }">
          <div>
            <el-switch v-model="row.enable" inline-prompt active-text="启用" @change="switchEnable(row.id, row.enable)"
              inactive-text="未启用" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="primary" @click="showUpdateModal(row.id)">编辑</el-button>
          <el-button type="danger" @click="delCate(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增商品的弹窗 -->
    <el-dialog v-model="addModalShow" title="新增商品" width="40%">
      <cate-add :cates="cates"/>
    </el-dialog>
    <!-- /新增商品弹窗 -->
     <!-- 修改商品的弹窗 -->
     <el-dialog v-model="updateModalShow" title="修改商品" width="40%">
      <cate-update :cates="cates" :id="cateId"/>
    </el-dialog>
    <!-- /修改商品弹窗 -->
  </div>
</template>

<script>
import { fetchCates, updateCate, delCate } from '@a/cateLists'
import SearchBar from '@c/SearchBar.vue'
import CateAdd from './components/CateAdd.vue'
import CateUpdate from './components/CateUpdate.vue'
export default {
  name: 'CateLists',
  data () {
    return {
      cateId: -1, // 修改商品id
      cates: [],
      searchParams: {
        key: '', // 搜索关键字
        start: '', // 开始日期
        end: '' // 结束日期
      },
      addModalShow: false,
      updateModalShow: false
    }
  },
  methods: {
    delCate (id) {
      // 确认框
      this.$confirm('此操作会彻底删除分类,继续吗?', '删除').then(() => {
        delCate(id).then(res => {
          if (res.data.code === 200) {
            this.$message({
              type: 'success',
              message: res.data.msg
            })
            this.fetchCates()
          }
        })
      })
    },
    showUpdateModal (id) {
      // 点击显示 修改商品弹窗
      this.updateModalShow = true
      this.cateId = id
    },
    showAddModal () {
      // 点击新增商品
      this.addModalShow = true
    },
    onSearch (searchParams) {
      this.searchParams = searchParams
      this.fetchCates()
    },
    switchEnable (id, enable) {
      // 行内修改商品分类选中状态  发请求修改
      console.log(id, enable)
      updateCate({
        id,
        enable
      }).then(res => {
        if (res.data.code === 200) {
          this.$message({
            type: 'success',
            message: '修改分类状态成功'
          })
          // 重新请求列表数据
          this.fetchCates()
        }
      })
    },
    fetchCates () {
      fetchCates({
        ...this.searchParams
      }).then(res => {
        if (res.data.code === 200) {
          this.cates = res.data.data
        }
      })
    }
  },
  created () {
    this.fetchCates()
  },
  components: {
    SearchBar,
    CateAdd,
    CateUpdate
  }
}
</script>

<style lang="scss" scoped></style>
